package qrcode;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;

public class QRVisualizer {

  public static Image toImage(QRCode qr, int moduleSize, int quietZoneSize) {
    final int imageWidth = qr.getWidth() * moduleSize + 2 * quietZoneSize;
    final int imageHeight = qr.getHeight() * moduleSize + 2 * quietZoneSize;
    BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = (Graphics2D) image.getGraphics();
    g.setBackground(Color.WHITE);
    g.clearRect(0, 0, imageWidth, imageHeight);
    for (int y = 0; y < qr.getHeight(); y++) {
      for (int x = 0; x < qr.getWidth(); x++) {
        g.setColor(Color.BLACK);
        if (qr.isBlack(x, y)) {
          g.fillRect(quietZoneSize + x * moduleSize, quietZoneSize + y * moduleSize, moduleSize, moduleSize);
        }
      }
    }
    return image;
  }
}